package cn.gotom.core.web.action;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import cn.gotom.core.web.Constants;

import com.opensymphony.xwork2.ActionSupport;

public class LogoutAction extends ActionSupport implements ServletResponseAware, ServletRequestAware
{

	private static final long serialVersionUID = 1L;
	protected final Logger log = Logger.getLogger(getClass());

	private HttpServletResponse response;
	private HttpServletRequest request;

	public String execute() throws Exception
	{
		HttpSession session = request.getSession(false);
		if (session != null)
		{
			session.removeAttribute(Constants.USER_SESSION_KEY);
		}
		Cookie[] cookies = request.getCookies();
		if (cookies != null)
		{
			for (Cookie cookie : cookies)
			{
				log.debug(cookie.getName() + "=" + cookie.getValue());
				if (Constants.COOKIE_REMEMBERME_KEY.equals(cookie.getName()))
				{
					cookie.setValue("");
					cookie.setMaxAge(0);
					response.addCookie(cookie);
					return Constants.LOGIN;
				}
			}
		}
		return Constants.LOGIN;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0)
	{
		request = arg0;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0)
	{
		response = arg0;
	}

}
